package cn.edu.zafu.bigdata.psy;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

public class UDPClient {
    public static void main(String[] args) {
        try {
            DatagramSocket clientSocket = new DatagramSocket();
            InetAddress serverAddress = InetAddress.getByName("192.168.16.73");
            int serverPort = 8888;

            byte[] sendData;
            byte[] receiveData = new byte[1024];

            sendData = "请求获取时间".getBytes();
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, serverAddress, serverPort);
            clientSocket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            clientSocket.receive(receivePacket);

            String timeString = new String(receivePacket.getData(), 0, receivePacket.getLength()).trim();
            System.out.println("服务器当前时间：" + timeString);

            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}